<template>
  <div class="login">
    <div class="login_box">
      <el-card>
        <h2>Login</h2>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="form.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="form.password"></el-input>
          </el-form-item>
        </el-form>
        <div class="btn">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info">注册</el-button>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      form: {
        username: 'admin',
        password: 'admin'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 15, message: '用户名长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 15, message: '密码长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login () {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('login', this.form)
        window.sessionStorage.setItem('token', res.data.token)
        window.sessionStorage.setItem('userId', res.data.id)
        window.sessionStorage.setItem('userName', res.data.username)
        console.log(res.data)
        const { data: res2 } = await this.$http.get(`getmyshop?token=${res.data.token}`, res.data.id)
        window.sessionStorage.setItem('number', res2.data.length)
        this.$router.push('/Home')
        window.location.reload()
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login{
  padding: 1rem;
  width: 100vw;
  height: 100vh;
  background-color: #ecf0f3;
  display: flex;
  justify-content: center;
  align-items: center;
  h2{
    font-size: 1rem;
    color: #333;
    text-align: center;
    padding-bottom: 1rem;
  }
  .login_box{
    .btn{
      display: flex;
      justify-content: flex-end;
    }
  }
}
.el-card{
  background-color: #ecf0f3;
  box-shadow: 10px 10px 10px #d1d9e6, -10px -10px 10px #f9f9f9;
  padding: 2rem;
  width: 20rem;
  height: 13rem;
}
.el-form{
  width: 100%;
  display: flex;
  flex-direction: column;
}
@media screen and (max-width:768px){
  .el-card{
    padding: 1rem;
  }
}
@media screen and (max-width:576px){
  .el-card{
    width: 15rem;
    height: 13rem;
    padding: 0;
  }
  .el-button{
    padding: 8px 12px;
    font-size: 12px;
  }
}
</style>
